#!/usr/bin/env python3
"""Generate V2 cover image - 1080x1920 vertical for Douyin"""

from PIL import Image, ImageDraw, ImageFont, ImageFilter
import os

OUT_DIR = r'E:\集群文件夹\factory_os\short_video_real_data_pipeline\phase4b_90s_formal_sample\v22_kimi_claude_loop\full_story_douyin_video\09_release\v2'
os.makedirs(OUT_DIR, exist_ok=True)

W, H = 1080, 1920

# Create dark tech-gradient background
img = Image.new('RGB', (W, H), (10, 12, 28))
draw = ImageDraw.Draw(img)

# Slow gradient (row by row)
for y in range(0, H, 4):
    brightness = 1.0 - (y / H) * 0.35
    r = int(12 * brightness)
    g = int(16 * brightness)
    b = int(38 * brightness)
    draw.rectangle([(0, y), (W, y+4)], fill=(r, g, b))

# Grid lines (every 80px)
for x in range(0, W, 80):
    draw.line([(x, 0), (x, H)], fill=(25, 30, 60), width=1)
for y in range(0, H, 80):
    draw.line([(0, y), (W, y)], fill=(25, 30, 60), width=1)

# Glow spots using ellipse with transparency
glow_layer = Image.new('RGBA', (W, H), (0, 0, 0, 0))
gdraw = ImageDraw.Draw(glow_layer)

for cx, cy, cr, color in [
    (540, 350, 400, (60, 80, 255, 30)),
    (200, 750, 250, (255, 60, 60, 20)),
    (880, 1100, 300, (255, 200, 0, 25)),
]:
    gdraw.ellipse([(cx-cr, cy-cr), (cx+cr, cy+cr)], fill=color)

glow_layer = glow_layer.filter(ImageFilter.GaussianBlur(radius=60))
img = Image.alpha_composite(img.convert('RGBA'), glow_layer).convert('RGB')
draw = ImageDraw.Draw(img)

# Load fonts
font_title = ImageFont.truetype('C:/Windows/Fonts/msyhbd.ttc', 76)
font_subtitle = ImageFont.truetype('C:/Windows/Fonts/msyh.ttc', 32)
font_score_label = ImageFont.truetype('C:/Windows/Fonts/msyh.ttc', 26)
font_number = ImageFont.truetype('C:/Windows/Fonts/msyhbd.ttc', 110)
font_number_small = ImageFont.truetype('C:/Windows/Fonts/msyhbd.ttc', 90)
font_tagline = ImageFont.truetype('C:/Windows/Fonts/msyh.ttc', 28)
font_stats_num = ImageFont.truetype('C:/Windows/Fonts/msyhbd.ttc', 50)

# Main title
title = "AI 自己做视频"
title_w = draw.textlength(title, font=font_title)
draw.text(((W - title_w) / 2, 400), title, fill=(220, 230, 255), font=font_title)

# Subtitle
sub = "Claude Code × Kimi 自动修复闭环"
sub_w = draw.textlength(sub, font=font_subtitle)
draw.text(((W - sub_w) / 2, 500), sub, fill=(100, 180, 255), font=font_subtitle)

# Score display: 69 → 92
# Left score (69 - red)
draw.text((300, 720), "69", fill=(255, 68, 68), font=font_number)

# Arrow
draw.text((470, 720), "→", fill=(255, 215, 0), font=font_number)

# Right score (92 - gold)
draw.text((600, 720), "92", fill=(255, 215, 0), font=font_number)

# Score labels
draw.text((300 - draw.textlength("初始评分", font=font_score_label)//2 + 55, 850),
          "初始评分", fill=(180, 180, 200), font=font_score_label)
draw.text((600 - draw.textlength("最终版本", font=font_score_label)//2 + 55, 850),
          "最终版本", fill=(255, 215, 0), font=font_score_label)

# Decorative line
line_y = 960
for x in range(200, W-200, 3):
    intensity = 1.0 - abs(x - W//2) / ((W-400)/2)
    r = int(40 + 80 * intensity)
    g = int(100 + 80 * intensity)
    b = int(200 + 55 * intensity)
    draw.rectangle([(x, line_y), (x+2, line_y+3)], fill=(r, g, b))

# Stats row
stat_data = [
    ("11", "轮迭代", 0),
    ("1", "次崩溃", 1),
    ("+23", "分提升", 2),
]
stat_w = W // 3
for num, label, idx in stat_data:
    sx = stat_w * idx + stat_w // 2
    draw.text((sx - draw.textlength(num, font=font_stats_num)//2, 1020),
              num, fill=(255, 255, 255), font=font_stats_num)
    draw.text((sx - draw.textlength(label, font=font_score_label)//2, 1085),
              label, fill=(150, 160, 180), font=font_score_label)

# Tagline
tagline = "这不是特效，这是真实工作流"
tagline_w = draw.textlength(tagline, font=font_tagline)
draw.text(((W - tagline_w) / 2, 1220), tagline, fill=(160, 190, 220), font=font_tagline)

# Bottom accent line
for x in range(200, W-200):
    progress = (x - 200) / (W - 400)
    r = int(0 + 100 * (1 - abs(progress - 0.5) * 2))
    g = int(60 + 120 * (1 - abs(progress - 0.5) * 2))
    b = int(160 + 80 * (1 - abs(progress - 0.5) * 2))
    h_y = 1600 + (int(progress * 20) % 3) * 6
    draw.point((x, h_y), fill=(r, g, b))

# Save
cover_path = os.path.join(OUT_DIR, 'cover_1080x1920.jpg')
img.save(cover_path, 'JPEG', quality=95)
print(f"Cover saved: {cover_path} ({os.path.getsize(cover_path)//1024}KB)")

# Also save in 09_release root
img.save(os.path.join(OUT_DIR, '..', 'cover_1080x1920.jpg'), 'JPEG', quality=95)
print(f"Cover also saved in release root")
